<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>async/await</title>
    <style>
        p{
            font-size: 20px;
            color: #000;
            margin: 10px 0;
            line-height: 2;
        }
        button{
            display: inline-block;
            min-width: 100px;
            height: 40px;
            border-radius: 10px;
            font-size: 18px;
            text-align: center;
            line-height: 40px;
        }
        pre{
            font-size: 18px;
        }
    </style>
</head>
<body>

<button onclick='show(1)'>async/await-完成</button>
<button onclick='show(0)'>async/await-拒绝</button>


</body>
<script>
// 异步返回
async function log(type) {
    if(type){
        return 'type is true'
    }else{
        throw 'type error'
    }
}
// 打印异步返回
async function show(type){
    // await log(type).then(function(e){
    //     console.log(e)
    // }).catch(function(e){
    //     console.error(e)
    // })
    log(type).then(function(e){
        console.log(e)
    }).catch(function(e){
        console.error(e)
    })
    console.log('虽然我在后面，但是我先执行')
}




function async_add(m, n, callback) {
   return new Promise(function(resolve, reject) {
     setTimeout(() => {
       resolve(callback.call(null, m + n));
     }, 500);
   })
 }

 async function addFn () {
    let res1 = await async_add(1, 2, function(num) {
      console.log(num);
      return num  // 一定要return
    });
    let res2 = await async_add(res1, 3, function(num) {
      console.log(num);
      return num
    })
    let res3 = await async_add(res2, 5, num => {
      console.log(num);
      return num;
    })
    let res4 = await async_add(res3, 10, num => {
      console.log(num);
      return num;
    })
    let res5 = await async_add(res4, 25, num => {
      console.log(num);
      return num;
    })
    let res6 = await async_add(res5, 37, num => {
      console.log(num);
      return num;
    })
 
    let res7 = await async_add(res6, 48, num => {
      console.log(num);
      return num;
    })

  }

</script>
</html>